package com.example.springbootdemo.database;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;


public class DataBase {

    static Connection connection;


    static DataBaseConfig config;

    static Connection getConnection() {
        if (config == null) {
            config = new DataBaseConfig();
        }
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass(config.driverClass);
            dataSource.setJdbcUrl(config.jdbcUrl);
            dataSource.setUser(config.user);
            dataSource.setPassword(config.password);
            dataSource.setMaxPoolSize(config.maxPoolSize);
            dataSource.setMinPoolSize(config.minPoolSize);
            connection = dataSource.getConnection();
            return connection;
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }


}
